/*
 * chimaera_commands.c:  the extends commands for managing project.
 *
 * ====================================================================
 * Copyright (c) 2000-2004 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 *
 * kkndyu@gmail.com
 * 2009.04.16
 * ====================================================================
 */

/* ==================================================================== */



/*** Includes. ***/

#include <string.h>
#include <assert.h>
#include <apr_strings.h>
#include <apr_hash.h>
#include <apr_md5.h>
#include "svn_wc.h"
#include "svn_ra.h"
#include "svn_delta.h"
#include "svn_subst.h"
#include "svn_client.h"
#include "svn_string.h"
#include "svn_pools.h"
#include "svn_error.h"
#include "svn_error_codes.h"
#include "svn_path.h"
#include "svn_io.h"
#include "svn_md5.h"
#include "svn_time.h"
#include "svn_sorts.h"
#include "svn_props.h"
#include "svn_iter.h"

#include "client.h"

#include "svn_private_config.h"


/*** Code. ***/

svn_error_t *
svn_client_chimaera_create_project(const char *project_name,
								   const char *root_path,
								   void *pinfo,
								   const char *group_leader,
								   const char *group,
								   apr_time_t *check_time_nodes,
								   apr_byte_t time_nodes_num,
								   const char *path_or_url,
								   svn_client_ctx_t *ctx,
								   apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
							ctx,pool);

	svn_ra_chimaera_create_project(ra_session, project_name,	root_path,	
						  pinfo, group_leader, group, check_time_nodes,
						  time_nodes_num, path_or_url, pool);




	return err;
}

svn_error_t *
svn_client_chimaera_get_userinfo(char **username,
					char **privilege,
					svn_revnum_t *recno,
					svn_string_t *user_ID,
					const char *path_or_url,
					svn_client_ctx_t *ctx,
 					apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;
	//const char *repos_root;

	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
							ctx,pool);
	//SVN_ERR(svn_ra_get_repos_root2(ra_session, &repos_root, pool));

	svn_ra_chimaera_get_userinfo(ra_session, username, privilege,	
							recno,user_ID,path_or_url, pool);

	return err;

}

svn_error_t *
svn_client_chimaera_get_newmessage(chimaeramessage_t **message,
								 apr_int32_t *num,
								 apr_int32_t user_recno,
								 const char *path_or_url,
								 svn_client_ctx_t *ctx,
								 apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	svn_ra_chimaera_get_newmessage(ra_session, message, num, 
					user_recno,	path_or_url, pool);

	return err;

}

svn_error_t *
svn_client_chimaera_get_userlist(chimaerauserlist_t **user_list,
								   apr_int32_t *num,
								   apr_int32_t sign,
								   const char *para,
								   const char *path_or_url,
								   svn_client_ctx_t *ctx,
								   apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_get_userlist(ra_session, user_list, num, 
		sign, para, path_or_url, pool);

	return err;

}

svn_error_t *
svn_client_chimaera_edit_columns(apr_int32_t sign,
								 svn_string_t *key,
								 svn_string_t *value1,
								 svn_string_t *value2,
								 svn_string_t *value3,
								 const char *path_or_url,
								 svn_client_ctx_t *ctx,
								 apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_edit_columns(ra_session, sign, key, 
		value1, value2, value3, path_or_url, pool);

	return err;
}

svn_error_t *
svn_client_chimaera_get_columns(apr_int32_t *group_recno,
								apr_int32_t sign,
								svn_string_t *key,
								svn_string_t *value1,
								const char *path_or_url,
								svn_client_ctx_t *ctx,
								apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_get_columns(ra_session, 
		group_recno, sign, key, value1, path_or_url, pool);

	return err;
}

svn_error_t *
svn_client_chimaera_get_latestrev(svn_revnum_t *latest_revnum,
								  const char *path_or_url,
								  svn_client_ctx_t *ctx,
								  apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_get_latest_revnum(ra_session, 
		latest_revnum, pool);

	return err;
}

svn_error_t *
svn_client_chimaera_remove_project(const char *path_or_url,
								   svn_client_ctx_t *ctx,
								   apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_remove_project(ra_session, 
		path_or_url, pool);

	return err;
}

svn_error_t *
svn_client_chimaera_insert_user(char *username, 
								svn_string_t *userid,
								apr_int32_t character, 
								svn_string_t *certificate,  
								char *userinfo,
								const char *path_or_url,
								svn_client_ctx_t *ctx,
								apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_insert_user(ra_session, username, 
		userid,character, certificate, 
		userinfo, path_or_url,pool);

	return err;
}

svn_error_t *
svn_client_chimaera_remove_user(svn_revnum_t user_recno,
								const char *path_or_url,
								svn_client_ctx_t *ctx,
								apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_remove_user(ra_session, 
		user_recno,path_or_url,pool);

	return err;
}

svn_error_t *
svn_client_chimaera_edit_user(svn_revnum_t user_recno,
							  char *username, 
							  svn_string_t *userid,
							  apr_int32_t character, 
							  svn_string_t *certificate,  
							  char *userinfo,
							  const char *path_or_url,
							  svn_client_ctx_t *ctx,
							  apr_pool_t *pool)
{
	svn_ra_session_t *ra_session;
	svn_error_t *err;


	/* Get an RA plugin for this filesystem object. */

	err = svn_client_open_ra_session(&ra_session,path_or_url,
		ctx,pool);


	err = svn_ra_chimaera_edit_user(ra_session, user_recno,
		username, userid,character, certificate, 
		userinfo, path_or_url,pool);

	return err;
}
